<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="/shop/include :: header"></head>
<body>
	<form class="layui-form" action="" lay-filter="shop_shuffling_figure-add-form" style="margin-top: 20px;">
		<!--  -->
		<div style="display: flex">
			<!--  -->
			<div class="layui-form-item" style="display: contents">
				<label class="layui-form-label">图片：</label>
				<div class="layui-upload-list" id="test1">
					<img class="layui-upload-img" id="demo1" style="width: 300px;height: 300px">
					<input type="hidden" name="imageAddress" id="imageAddress">
				</div>
			</div>
			<div style="width: 63%">
				<!--  -->
				<div class="layui-form-item">
					<label class="layui-form-label">图片链接：</label>
					<div class="layui-input-block">
						<input id="imageUrl" name="imageUrl" placeholder="请输入图片链接"
							   class="layui-input" lay-verify="required" autocomplete="off">
					</div>
				</div>
				<!--  -->
				<div class="layui-form-item">
					<label class="layui-form-label">图片权重 权重越大越靠前：</label>
					<div class="layui-input-block">
						<input id="weight" name="weight" placeholder="请输入图片权重 权重越大越靠前"
							   class="layui-input" lay-verify="required" autocomplete="off">
					</div>
				</div>
			</div>
		</div>
 		<div class="layui-form-item" style="text-align: center">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit="" lay-filter="shop_shuffling_figure-add">立即提交</button>
			</div>
		</div>
	</form>
	<div th:include="/shop/include::footer"></div>
	<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
	<script>
		layui.use([ 'layer', 'jquery', 'form', 'laydate','upload'], function() {
			var $ = layui.jquery;
			var laydate = layui.laydate;
			var layer = layui.layer;
			var form = layui.form;
			var upload = layui.upload;

			var uploadInst = upload.render({
				elem: '#test1'
				, url: '/platform/UploadFile' //改成您自己的上传接口
				//, headers: {"token": layui.data(layui.setter.tableName)['token']}
				, before: function (obj) {
					//预读本地文件示例，不支持ie8
					obj.preview(function (index, file, result) {
						$('#demo1').attr('src', result); //图片链接（base64）
					});
				}
				, done: function (res) {
					//如果上传失败
					if (res.status != "SUCCESS") {
						return layer.msg('上传失败');
					}
					//上传成功
					layer.msg('上传成功');
					$("#imageAddress").val(res.data)
				}
				, error: function () {
					//演示失败状态，并实现重传
					var demoText = $('#demoText');
					demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
					demoText.find('.demo-reload').on('click', function () {
						uploadInst.upload();
					});
				}
			});

  			//监听提交
			form.on('submit(shop_shuffling_figure-add)', function(data) {
				$.ajax({
					url: '/platform/shufflingfigure/add',
					type: "post",
					data: JSON.stringify(data.field),
					contentType: 'application/json; charset=utf-8', // 很重要
					async: false,
					success: function (result) {
						if (result.status == "SUCCESS"){
							layer.msg(result.message, {
								offset: '15px'
								, icon: 1
								, time: 1000
							}, function () {
								window.parent.location.reload();
							});
						}else {
							layer.msg(result.message)
						}
					}
				});
				return false;
			});
		});
	</script>
</body>
</html>